<template>
  <div class="main">
    <FarmerSelectTools/>
    <el-divider>查询结果</el-divider>
    <el-table
        :data="allFarmer"
        style="width: 100%">
      <el-table-column
          label="用户名"
          width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.user.username }}</span>
        </template>
      </el-table-column>
      <el-table-column
          label="姓名"
          width="180">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>性别: {{ scope.row.sex }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.name }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
          label="电话号码"
          width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column
          label="快递地址"
          width="260">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.address }}</span>
        </template>
      </el-table-column>
      <el-table-column
          label="邮箱地址"
          width="220">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.email }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-dialog
              title="提示"
              :visible.sync="dialogVisible"
              width="30%"
              :before-close="handleClose">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
              <el-form-item label="账号" prop="username">
                <el-input type="text" v-model="ruleForm.username"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input type="text" v-model="ruleForm.password"></el-input>
              </el-form-item>
              <el-form-item label="真实姓名" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
              </el-form-item>
              <el-form-item label="性别" prop="sex">
                <el-input v-model="ruleForm.sex"></el-input>
              </el-form-item>
              <el-form-item label="电话" prop="phone">
                <el-input v-model="ruleForm.phone"></el-input>
              </el-form-item>
              <el-form-item label="电子邮件" prop="email">
                <el-input v-model="ruleForm.email"></el-input>
              </el-form-item>
              <el-form-item label="快递地址" prop="address">
                <el-input v-model="ruleForm.address"></el-input>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
            </span>
          </el-dialog>

          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

</template>

<script>
import FarmerSelectTools from "@/components/admin/FarmerSelectTools";
import {mapActions, mapState} from "vuex";

export default {
  name:'UserList',
  components:{
    FarmerSelectTools,
  },
  data() {
    return {
      dialogVisible: false,
      ruleForm: {
        id:null,
        uid:null,
        username: '',
        password: '',
        name: '',
        sex: '',
        phone: '',
        email: '',
        address: '',
      },
      rules: {
        username: [
          { required: true,message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true,message: '请输入密码', trigger: 'blur' }
        ],
        name: [
          { required: true,message: '请输入真实姓名', trigger: 'blur' }
        ],
        sex: [
          { required: true,message: '请选择性别', trigger: 'blur' }
        ],
        phone: [
          { required: true,message: '请输入电话号码', trigger: 'blur' }
        ],
        email: [
          { required: true,message: '请输入电子邮件', trigger: 'blur' }
        ],
        address: [
          { required: true,message: '请输入快递地址', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    handleEdit(index, row) {
      this.dialogVisible = true
      this.ruleForm.id = row.id
      this.ruleForm.uid = row.uid
      this.ruleForm.username=row.user.username
      this.ruleForm.password=row.user.password
      this.ruleForm.name=row.name
      this.ruleForm.sex=row.sex
      this.ruleForm.phone=row.phone
      this.ruleForm.email=row.email
      this.ruleForm.address=row.address
    },
    handleDelete(index, row) {
      this.delFarmerById(row.id)
      this.delUserById(row.uid)
    },
    handleClose(done) {
      this.$confirm('当前信息未被更新，是否退出？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.changeFarmer(this.ruleForm)
          this.$message.success("修改成功")
          this.findAllFarmer()
        } else {
          console.log('error submit!!');
          return false;
        }
        this.dialogVisible = false
      })
    },
    ...mapActions('farmer',{findAllFarmer:'findAllFarmer',changeFarmer:'changeFarmer',delFarmerById:'delFarmerById'}),
    ...mapActions('user',{delUserById:'delUserById'})
  },
  computed:{
    ...mapState('farmer',['allFarmer'])
  },
  async created() {
    await this.findAllFarmer()
  }
}
</script>

<style scoped>
  .main{
    margin: 50px 20px 20px;
  }
</style>